package com.ymatou.iapi.couponmanage.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.coupon.parameter.UseCouponBean;
import com.ymatou.iapi.coupon.parameter.userReceiveCouponBagBean;
import com.ymatou.iapi.coupon.service.UseCouponCall;
import com.ymatou.iapi.coupon.service.userReceiveCouponBagCall;
import com.ymatou.iapi.couponmanage.parameter.CreateBagBean;
import com.ymatou.iapi.couponmanage.parameter.InvalidBean;
import com.ymatou.iapi.couponmanage.service.CreateBagCall;
import com.ymatou.iapi.couponmanage.service.InvalidCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallServiceV2;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.coupon;
import com.ymttest.database.model.couponbag;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.database.sqlwapper.couponWapper;
import com.ymttest.database.sqlwapper.couponbagWapper;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.utils.TestDataManager;
/**
*作废套券
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_invalid{
	private static InvalidBean invalidBean;
	private static InvalidCall invalidCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("作废套券");
	}

	@Before
	public void caseUp() {
		invalidBean=new InvalidBean();
		invalidCall=new InvalidCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(invalidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_invalid_001() {
		Logger.start(true,"作废套券");
		try {
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			CreateBagBean createBagBean = CouponServiceMCallServiceV2.setDefaultPlatformCreateCouponBagBeanForRecieve();
			CreateBagCall createBagCall = CouponServiceMCallServiceV2.createPlatformCouponBag(createBagBean);
			
			invalidBean.setBagCode(createBagCall.getbagCode());
			invalidBean.setInvalidMemo("autoTest");
			invalidBean.setInvalidUser("11");
			invalidCall.setData(invalidBean);
			invalidCall.callService();			
			
			Logger.verifyEquals("操作成功！", invalidCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("0", invalidCall.getStatusCode1(), "验证返回StatusCode");
			
			//验证数据库
			couponbag batchbag = new couponbagWapper().selectByPrimaryKey(createBagCall.getbagCode());
			List<couponbatch> couponbatch=new couponbatchWapper().getCouponBatchByBagCode(batchbag.getBagCode());
			
			Logger.verifyEquals(true, batchbag.getIsInvalid(), "验证数据库batchbag表中这条记录IsInvalid=true");
			for(couponbatch batch : couponbatch)
			{
				int count=+1;
				Logger.verifyEquals(true, batch.getIsInvalid(), "验证数据库couponbatch表中第"+count+"条这条记录IsInvalid=true");
			}	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_invalid_002() {
		Logger.start(true,"作废套券-套券编码不存在(本来提示错误信息，但研发说作废之前不一定要查询套券是否存在的)");
		try {
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			CreateBagBean createBagBean = CouponServiceMCallServiceV2.setDefaultPlatformCreateCouponBagBeanForRecieve();
			CreateBagCall createBagCall = CouponServiceMCallServiceV2.createPlatformCouponBag(createBagBean);
			
			invalidBean.setBagCode(createBagCall.getbagCode());
			invalidBean.setInvalidMemo("autoTest");
			invalidBean.setInvalidUser("11");
			invalidCall.setData(invalidBean);
			invalidCall.callService();				
			Logger.verifyEquals("操作成功！", invalidCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("0", invalidCall.getStatusCode1(), "验证返回StatusCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_invalid_003() {
		Logger.start(true,"作废套券-套券部分被领取");
		try {
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			CreateBagBean createBagBean = CouponServiceMCallServiceV2.setDefaultPlatformCreateCouponBagBeanForRecieve();
			CreateBagCall createBagCall = CouponServiceMCallServiceV2.createPlatformCouponBag(createBagBean);
			
			userReceiveCouponBagBean userReceiveCouponBagBean=CouponServiceMCallServiceV2.setuserReceiveCouponBagBean(createBagCall.getbagCode());
			CouponServiceMCallServiceV2.userReceiveCouponBagCall(userReceiveCouponBagBean);
			
			invalidBean.setBagCode(createBagCall.getbagCode());
			invalidBean.setInvalidMemo("autoTest");
			invalidBean.setInvalidUser("11");
			invalidCall.setData(invalidBean);
			invalidCall.callService();				
			Logger.verifyEquals("操作成功！", invalidCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("0", invalidCall.getStatusCode1(), "验证返回StatusCode");		
			//验证数据库
			couponbag batchbag = new couponbagWapper().selectByPrimaryKey(createBagCall.getbagCode());
			List<couponbatch> couponbatch=new couponbatchWapper().getCouponBatchByBagCode(batchbag.getBagCode());
			
			Logger.verifyEquals(true, batchbag.getIsInvalid(), "验证数据库batchbag表中这条记录IsInvalid=true");
			for(couponbatch batch : couponbatch)
			{
				int count=+1;
				Logger.verifyEquals(true, batch.getIsInvalid(), "验证数据库couponbatch表中第"+count+"条这条记录IsInvalid=true");
			}	
			
			List<coupon> coupons=new couponWapper().selectbyBagCode(batchbag.getBagCode());
			for(coupon c:coupons)
			{
				int count=+1;
				Logger.verifyEquals(false, c.getIsInvalid(), "验证数据库coupon表中第"+count+"条这条记录IsInvalid=false");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_invalid_008() {
		Logger.start(true,"作废套券-套券中的部分优惠券已被使用");
		try {
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			CreateBagBean createBagBean = CouponServiceMCallServiceV2.setDefaultPlatformCreateCouponBagBeanForRecieve();
			CreateBagCall createBagCall = CouponServiceMCallServiceV2.createPlatformCouponBag(createBagBean);
			
			userReceiveCouponBagBean userReceiveCouponBagBean=CouponServiceMCallServiceV2.setuserReceiveCouponBagBean(createBagCall.getbagCode());
			userReceiveCouponBagBean.setUserId(userId);
			userReceiveCouponBagCall userReceiveCouponBagCall=CouponServiceMCallServiceV2.userReceiveCouponBagCall(userReceiveCouponBagBean);
			
			List<String> CouponCodes=new ArrayList();
			List<coupon> coupons1=new couponWapper().selectbyBagCode(userReceiveCouponBagBean.getCouponBagCode());
			for(coupon coupon:coupons1)
			{
		    	CouponCodes.add(coupon.getCouponCode());
			}
			
			UseCouponBean usecouponBean=new UseCouponBean();
			usecouponBean.setUserId(userId);
			usecouponBean.setMainOrderId(orderId);
			usecouponBean.setCouponCodes(CouponCodes);
			usecouponBean.setAppId("mansit");
			usecouponBean.setRequestId("12344566");
			
			UseCouponCall useCouponCall=new UseCouponCall();
			useCouponCall.setData(usecouponBean);
			useCouponCall.callService();
			
			invalidBean.setBagCode(userReceiveCouponBagBean.getCouponBagCode());
			invalidBean.setInvalidMemo("autoTest");
			invalidBean.setInvalidUser("11");
			invalidCall.setData(invalidBean);
			invalidCall.callService();			
			
			//验证数据库
			couponbag batchbag = new couponbagWapper().selectByPrimaryKey(createBagCall.getbagCode());
			List<couponbatch> couponbatch=new couponbatchWapper().getCouponBatchByBagCode(batchbag.getBagCode());
			
			Logger.verifyEquals(true, batchbag.getIsInvalid(), "验证数据库batchbag表中这条记录IsInvalid=true");
			for(couponbatch batch : couponbatch)
			{
				int count=+1;
				Logger.verifyEquals(true, batch.getIsInvalid(), "验证数据库couponbatch表中第"+count+"条这条记录IsInvalid=true");
			}	
			
			List<coupon> coupons=new couponWapper().selectbyBagCode(batchbag.getBagCode());
			for(coupon c:coupons)
			{
				int count=+1;
				Logger.verifyEquals(false, c.getIsInvalid(), "验证数据库coupon表中第"+count+"条这条记录IsInvalid=false");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_invalid_004() {
		Logger.start(true,"作废套券-作废已作废的套券");
		try {
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			CreateBagBean createBagBean = CouponServiceMCallServiceV2.setDefaultPlatformCreateCouponBagBeanForRecieve();
			CreateBagCall createBagCall = CouponServiceMCallServiceV2.createPlatformCouponBag(createBagBean);
		
			invalidBean.setBagCode(createBagCall.getbagCode());
			invalidBean.setInvalidMemo("autoTest");
			invalidBean.setInvalidUser("11");
			invalidCall.setData(invalidBean);
			invalidCall.callService();	
			invalidCall.callService();	
			
			Logger.verifyEquals("操作成功！", invalidCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("0", invalidCall.getStatusCode1(), "验证返回StatusCode");
			
			//验证数据库
			couponbag batchbag = new couponbagWapper().selectByPrimaryKey(createBagCall.getbagCode());
			List<couponbatch> couponbatch=new couponbatchWapper().getCouponBatchByBagCode(batchbag.getBagCode());
			
			Logger.verifyEquals(true, batchbag.getIsInvalid(), "验证数据库batchbag表中这条记录IsInvalid=true");
			for(couponbatch batch : couponbatch)
			{
				int count=+1;
				Logger.verifyEquals(true, batch.getIsInvalid(), "验证数据库couponbatch表中第"+count+"条这条记录IsInvalid=true");
			}			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Ts_invalid_005() {
		Logger.start(true,"作废套券-invalidUser为空");
		try {
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			CreateBagBean createBagBean = CouponServiceMCallServiceV2.setDefaultPlatformCreateCouponBagBeanForRecieve();
			CreateBagCall createBagCall = CouponServiceMCallServiceV2.createPlatformCouponBag(createBagBean);
		
			invalidBean.setBagCode(createBagCall.getbagCode());
			invalidBean.setInvalidMemo("autoTest");
			invalidBean.setInvalidUser(null);
			invalidCall.setData(invalidBean);
			invalidCall.callService();		
			
			Logger.verifyEquals(true, invalidCall.getMessage().contains("作废人不能为空"), "验证返回Message");
			Logger.verifyEquals("1", invalidCall.getStatusCode1(), "验证返回StatusCode");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Ts_invalid_006() {
		Logger.start(true,"作废套券-invalidMemo为空");
		try {
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			CreateBagBean createBagBean = CouponServiceMCallServiceV2.setDefaultPlatformCreateCouponBagBeanForRecieve();
			CreateBagCall createBagCall = CouponServiceMCallServiceV2.createPlatformCouponBag(createBagBean);
		
			invalidBean.setBagCode(createBagCall.getbagCode());
			invalidBean.setInvalidMemo(null);
			invalidBean.setInvalidUser("11");
			invalidCall.setData(invalidBean);
			invalidCall.callService();		
			
			Logger.verifyEquals(true, invalidCall.getMessage().contains("作废理由不能为空"), "验证返回Message");
			Logger.verifyEquals("1", invalidCall.getStatusCode1(), "验证返回StatusCode");			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Ts_invalid_007() {
		Logger.start(true,"作废套券-bagCode为空");
		try {
		
			invalidBean.setBagCode(null);
			invalidBean.setInvalidMemo("autoTest");
			invalidBean.setInvalidUser("11");
			invalidCall.setData(invalidBean);
			invalidCall.callService();		
			
			Logger.verifyEquals(true, invalidCall.getMessage().contains("必须传入套券编号"), "验证返回Message");
			Logger.verifyEquals("1", invalidCall.getStatusCode1(), "验证返回StatusCode");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}